const fs = require("fs");
const path = require("path");
const encoding = "utf-8";

const getContent = (filename) => {
  const dir = path.resolve(process.cwd(), "environments");
  return fs.readFileSync(path.resolve(dir, filename), { encoding });
};

const jsonParse = (obj) => {
  return Function('"use strict";return (' + obj + ")")();
};

const getConfig = () => {
  try {
    return jsonParse(getContent("proxy-config.json"));
  } catch (e) {
    return {};
  }
};

module.exports = {
  proxy: {
    // 接口匹配规则自行修改
    "/api": {
      // 优先使用router函数里的url，没有再取target
      target: "http://dev.center.xiongmaodangao.com",
      changeOrigin: true,
      ws: true,
      pathRewrite: {
        '^/api': '/api'
      },
      router: () => (getConfig() || {}).target || "", // 修改proxy-config.json中的target热生效，无需重启
    },
  },
};
